package modele;
import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;


@Entity
public class Membre {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	
	private String nom;
	private String prenom;
	
	@Field(index=Index.UN_TOKENIZED) 
	private String pseudo;
	
	private String mail;
	private String password;
	// On ne stock pas les images dans la base
	// celle-ci sont sur le serveur, on 
	// conserve le chemin pour y acceder
	// ex. : image/mon_image.jpg
	private String image;
	
	private boolean droit_rediger;
	private boolean droit_commenter;
	private boolean droit_admin;
	private int points;
	
	// Compte activé?
	private boolean active;
	
	// cle permetant d'activer le compte
	private String cle;
	
	@ManyToOne
	private Competition Competitions_admin;
	
	@OneToMany(mappedBy="auteur",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<Article> articles;
	
	@OneToMany(mappedBy="membre",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<NoteArticle> notes;
	
	@OneToMany(mappedBy="auteur",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private Collection<Commentaire> commentaires;
	
	@OneToMany(mappedBy="membre",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private  Collection<ApprobationCommentaire> approbationscommentaires;
	
	@OneToMany(mappedBy="receveurNotification",fetch=FetchType.EAGER)
	@Fetch(FetchMode.SUBSELECT)
	private  Collection<Notification> notifications;
	
	private int nbNotificationsNonVues;
	
	public Membre(){
		this.approbationscommentaires= new ArrayList<ApprobationCommentaire>();
		this.articles= new ArrayList<Article>();
		this.commentaires = new ArrayList<Commentaire>();
		this.notes = new ArrayList<NoteArticle>();
		this.notifications = new ArrayList<Notification>();
		this.setNbNotificationsNonVues(0);
	}

	public Collection<Article> getArticles() {
		return articles;
	}

	public void setArticles(Collection<Article> articles) {
		this.articles = articles;
	}

	public Collection<NoteArticle> getNotes() {
		return notes;
	}

	public void setNotes(Collection<NoteArticle> notes) {
		this.notes = notes;
	}

	public Collection<Commentaire> getCommentaires() {
		return commentaires;
	}

	public void setCommentaires(Collection<Commentaire> commentaires) {
		this.commentaires = commentaires;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getPseudo() {
		return pseudo;
	}

	public void setPseudo(String pseudo) {
		this.pseudo = pseudo;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getImage() {
		return image;
	}
	
	public void setCompetitions_admin(Competition competitions_admin) {
		Competitions_admin = competitions_admin;
	}

	public Competition getCompetitions_admin() {
		return Competitions_admin;
	}

	public void setApprobationscommentaires(Collection<ApprobationCommentaire> approbationscommentaires) {
		this.approbationscommentaires = approbationscommentaires;
	}

	public Collection<ApprobationCommentaire> getApprobationscommentaires() {
		return approbationscommentaires;
	}
	
	public String getCle(){
		return this.cle;
	}
	
	public void setCle(String c){
		this.cle = c;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public int getPoints() {
		return points;
	}

	public void setDroit_admin(boolean droit_admin) {
		this.droit_admin = droit_admin;
	}

	public boolean isDroit_admin() {
		return droit_admin;
	}

	public void setDroit_commenter(boolean droit_commenter) {
		this.droit_commenter = droit_commenter;
	}

	public boolean isDroit_commenter() {
		return droit_commenter;
	}

	public void setDroit_rediger(boolean droit_rediger) {
		this.droit_rediger = droit_rediger;
	}

	public boolean isDroit_rediger() {
		return droit_rediger;
	}

	public void augmenterPoints(int pts){
		this.points+=pts;
		
		if (this.points>Config.nbPointAdmin){
			this.droit_commenter=this.droit_rediger=this.droit_admin=true;
			return;
		}
		else if (this.points>Config.nbPointsRediger){
			this.droit_commenter=this.droit_rediger=true;
			this.droit_admin=false;
			return;
		}
		else if (this.points>Config.nbPointsCommenter){
			this.droit_commenter=true;
			this.droit_rediger=this.droit_admin=false;
			return;
		}
		else{
			this.droit_commenter=this.droit_rediger=this.droit_admin=false;
			return;
		}
	}

	public Collection<Notification> getNotifications() {
		return notifications;
	}

	public void setNotifications(Collection<Notification> notifications) {
		this.notifications = notifications;
	}

	public void ajouterNotification(Notification n){
		this.nbNotificationsNonVues++;
		this.notifications.add(n);
	}
	
	public void setNbNotificationsNonVues(int nbNotificationsNonVues) {
		this.nbNotificationsNonVues = nbNotificationsNonVues;
	}

	public int getNbNotificationsNonVues() {
		return nbNotificationsNonVues;
	}
	
	public Collection<Notification> lireNotifications(){
		this.nbNotificationsNonVues=0;
		return this.notifications;
	}

}